from django.db import models

# Create your models here.

from django.db import models
from django.utils import timezone

from base.models import BaseVmodel

from ckeditor.fields import RichTextField


class BookType(BaseVmodel):
    type_name = models.CharField(max_length=200, verbose_name='类名', db_index=True)
    parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, verbose_name='父级')

    class Meta:
        verbose_name = '图书类型'
        verbose_name_plural = verbose_name
        db_table = 't_book_type'

    def __str__(self):
        return self.type_name


class Book(BaseVmodel):
    title = models.CharField(max_length=200, verbose_name='标题')
    book_type = models.ForeignKey(BookType, on_delete=models.CASCADE, verbose_name='图书类型')
    author = models.CharField(max_length=100, verbose_name='作者')
    isbn = models.CharField(max_length=13, unique=True, verbose_name='ISBN')
    is_published = models.BooleanField(default=True, verbose_name='是否发布')
    published_date = models.DateField(default=timezone.now, verbose_name='出版日期')
    quantity = models.IntegerField(default=1, verbose_name='库存数量')
    intro = models.TextField(blank=True, null=True, verbose_name='简介')
    content = RichTextField(blank=True, null=True, verbose_name='内容')
    download_url = models.FileField(upload_to='book/', blank=True, null=True, verbose_name='下载地址')

    class Meta:
        verbose_name = '图书'
        verbose_name_plural = verbose_name
        db_table = 't_book'

    def __str__(self):
        return self.title
